﻿using System;

namespace ZquenceStudio3Host.DspPlugins.Synthesizers.Nodesizer.Engine.Modules.HighLevel
{
    /// <summary>
    /// Splitter module that can splitt any source to two output sources.
    /// </summary>
    [ModuleDisplay("Split")]
    public class Splitter2Module : BaseModule
    {
        public readonly ModuleInput Input;
        public readonly ModuleOutput Out1;
        public readonly ModuleOutput Out2;

        public Splitter2Module(ModuleManager manager)
            : base(manager)
        {
            // Add input and outputs to module.
            AddInput(Input = new ModuleInput("In", IOType.Audio, this));
            AddOutput(Out1 = new ModuleOutput("Out1", IOType.Audio, this));
            AddOutput(Out2 = new ModuleOutput("Out2", IOType.Audio, this));
        }

        public override void Process()
        {
            // First check if the input source is connected.
            if (Input.IsConnected)
            {
                // Now check if the out1 soource is connected, if yes when copy the input source to the output.
                if (Out1.IsConnected)
                {
                    Array.Copy(Input.FromOutput.AudioBuffer.Buffer, Out1.AudioBuffer.Buffer, Out1.AudioBuffer.Length);
                }

                // And do the same with the out2 is it is connected.
                if (Out2.IsConnected)
                {
                    Array.Copy(Input.FromOutput.AudioBuffer.Buffer, Out2.AudioBuffer.Buffer, Out2.AudioBuffer.Length);
                }
            }
        }
    }
}
